<?php
/**
 * chartNode holds node data and references to child nodes. $parent is the id of
 * the parent, not a reference to a parent node.
 * A node searches for its children and outputs its data for the JavaScript.
 *
 * Ivan Bliskavka
 */
class chartNode
{
    public $id, $parent, $name, $department, $title, $childNodes;

    public function __construct($iId, $iParent, $iName, $iDepartment, $iTitle)
    {
        //iId=Employee ID, iParent=Employee supervisor's employee id.
        $this->id = $iId;
        $this->parent = $iParent;

        //Escape ' for use in JavaScript
        $this->name = str_replace("'", "\\'", $iName);
        $this->department = str_replace("'", "\\'", $iDepartment);
        $this->title = str_replace("'", "\\'", $iTitle);
    }
    public function getChildNodes(&$inArr, $depth, &$nodeIndexes)
    {
        //recursive function to collect childNodes

        $nodeIndexes[] = $this->id;

        //Find childNodes for current node
        if(!empty($inArr))
        {
            foreach ($inArr as $id => $node)
            {
                if($node->parent == $this->id)
                {
                    $this->childNodes[$id] = $node;
                }
            }
        }
        //Call this function for all childNodes
        if (!empty($this->childNodes) && $depth-1 > 0)
        {
            foreach ($this->childNodes as $id => $node)
            {
                $node->getChildNodes($inArr, $depth - 1, $nodeIndexes);
            }
        }
    }

    //-- Section Start: Output JavaScript --\\
    public function getJS(&$nodeItems)
    {
        $nodeItems .= $this->outputForJs();
       
        //Call this function for all childNodes
        if (!empty($this->childNodes))
        {
            foreach ($this->childNodes as $id => $node)
            {
                $node->getJS($nodeItems);
            }
        }
    }
    private function outputForJs()
    {
        //Prints the JavaScript node entry. Was not included in the object constructor because not all nodes will output.

        //nodeContents: Data to be displayed inside the node
        //linkTitle: Will be displayed on subCharts.
        $nodeContents = "'";
        $linkTitle = "";
        if($GLOBALS['config']->showName && $this->name != Null)
        {
            $nodeContents .= sprintf($GLOBALS['config']->nameFormat, $this->name);
            $linkTitle .= $this->name."<br />";
        }
        if($GLOBALS['config']->showDiv && $this->department != Null)
        {
            $nodeContents .= sprintf($GLOBALS['config']->divFormat, $this->department);
            $linkTitle .= $this->department."<br />";
        }
        if($GLOBALS['config']->showTitle && $this->title != Null)
        {
            $nodeContents .= sprintf($GLOBALS['config']->titleFormat, $this->title);
            $linkTitle .= $this->title;
        }
        $nodeContents .= "'";
        $linkTitle = str_replace("&", "%26", $linkTitle);

        //Actual JS output. There are additional features here that are currently no used (null). Refer to ECOTree.js.
        $nodeFormat = "\t\t\t\tt.add(%d,%d,%s, null, null, null, null, 'index.php?root=%d&depth=%d&title=%s');\n";
        return sprintf($nodeFormat, $this->id, $this->parent, $nodeContents, $this->id, 100, htmlentities($linkTitle));
    }
    //-- Section End --\\
}
?>
